﻿using System;
using System.Collections.Generic;

using System.Text;
using Xeno.Core;
using Xeno.Module;
using OpenTK;
using OpenTK.Graphics;
using OpenTK.Graphics.OpenGL;
using OpenTK.Math;
using PhysX.NET;
namespace Xeno.Physics
{
    public class XenoBoxCollider : XenoCollider
    {

        public NxBoxShapeDesc Box { get; set; }
        public XenoBoxCollider(string name,XenoNode attached,bool autoFit) :base(name,attached)
        {

         
            List<XenoNode> meshes = attached.AllWithType(typeof(XenoMesh), null);

            XenoMesh mesh = meshes[0].Mesh;


            Vector3[] bb = attached.BoundingBox;
            if (bb[0].Y == bb[1].Y)
            {
                bb[1].Y += 1f;
            }
            
            List<XenoNode> managers = Attached.GraphOwner.RootNode.AllWithType(typeof(XenoPhysicsManager), null);
            XenoNode man = managers[0];

            BoundingBox = bb;

            NxVec3 dim = new NxVec3();
            dim.x = bb[1].X - bb[0].X;
            dim.y = bb[1].Y - bb[0].Y;
            dim.z = bb[1].Z - bb[0].Z;
            dim.x *= 0.5f;
            dim.y *= 0.5f;
            dim.z *= 0.5f;

            NxVec3 pos = new NxVec3();

            pos.x = bb[0].X + ((bb[1].X - bb[0].X) / 2.0f);
            pos.y = bb[0].Y + ((bb[1].Y - bb[0].Y) / 2.0f);
            pos.z = bb[0].Z + ((bb[1].Z - bb[0].Z) / 2.0f);


            Box = new NxBoxShapeDesc();
            Box.dimensions = dim;
            NxMat34 mat = new NxMat34();
            mat.t = pos;
            Shape = Box as NxShapeDesc;

            Box.localPose = mat;
            
        }
    }
}
